<?php
namespace app\system\controller;
use \think\Controller;
use think\Db;
use think\Session;
use think\Request;
use think\Cookie;

class Message extends Base{
    use \app\traits\tablename\Member;
    public function _initialize() {
        parent::_initialize ();
    }


    /**
     * 消息发送
     * modular_type => 模块类型 1=>验证码 2=> 订单 ...  必须
     * msg_type => 1=> 'sms'短信  2=>'email' 邮箱  必须
     * template_id => 模板id  否
     * push_uid => 接收人uid 0即是平台的 必须
     * sms_content =>['O20170726143350722484','10'] 发送内容  订单号  时间 否
     * email_subject => 邮箱标题
     * email_message => 邮箱内容
     *
     */

    public function push_msg($push_msg=[]){
        try{
            $data = !empty($push_msg)?$push_msg:$this->data;
            $is_front = !empty($data['is_front'])?true:false;
            $check = $this->validate($data,'system/Message.check_modular_type');
            if($check !== true){
                return api_return($check);
            }
//            echo json_encode($data['msg_type']);exit;
            foreach($data['msg_type'] as $k => $v){
                if($k === 'sms'){    //短信发送
                    if(empty($v['sms_content'])){
                        return api_return(-11768);
                    }
                    $db_hanlder =  db::connect('database.db_config2');
                    $template_id = $db_hanlder->name('sms_template')->where(['type'=>$data['modular_type'],'web_id'=>0,'lang_id'=>config('lang_id')])->order('addtime desc')->find();
                    //检测订单号
                    $is_exist_on = db::name(self::$member_tb['Order'])->where(['order_no'=>$v['sms_content'][0]])->find();
                    if(empty($is_exist_on)){
                        return api_return(-10405);
                    }
                    if($is_front){//前端发送  shop_uid uid
                        if(empty($data['push_uid'])){
                            $plate_uid  =  $db_hanlder->name('domain')->field('uid')->where(['web_id'=>$data['web_id']])->find();
                            $push_mobile =  $db_hanlder->name('admin')->where(['uid'=>$plate_uid['uid']])->value('phone');
                        }else{
                            $push_mobile =  db::name(self::$member_tb['member'])->where(['uid'=>$data['push_uid']])->value('mobile');
                        }
                        single_send_sms($push_mobile,$v['sms_content'],$template_id['id']??8,$param=[]);
                    }else{
                        if(empty($data['push_uid'])){
                            return api_return(-10301);
                        }

                        $push_mobile = db::name(self::$member_tb['member'])->where(['uid'=>$data['push_uid']])->value('mobile');
                        single_send_sms($push_mobile,$v['sms_content'],$template_id['id']??8,$param=[]);
                    }
                }elseif($k === 'email'){
                    if(empty($v['email_subject'])){
                        return api_return(-11769);
                    }
                    if(empty($v['email_message'])){
                        return api_return(-11770);
                    }

                    if($is_front){//前端发送  shop_uid uid
                        //$send_mobile = db::name(self::$member_tb['member'])->where(['uid'=>$data['send_uid']])->value('mobile');
                        if(empty($data['push_uid'])){
                            $db_hanlder =  db::connect('database.db_config2');
                            $plate_uid  =  $db_hanlder->name('domain')->field('uid')->where(['web_id'=>$data['web_id']])->find();
                            $push_email =  $db_hanlder->name('admin')->where(['uid'=>$plate_uid['uid']])->value('email');
                        }else{
                            $push_email =  db::name(self::$member_tb['member'])->where(['uid'=>$data['push_uid']])->value('email');
                        }
                        single_send_mail($push_email, $v['email_subject'], $v['email_message'], $template = null , $attachment = null , $ssl = false , $from = null,$param=[]);
                    }else{
                        if(empty($data['push_uid'])){
                            return api_return(-10301);
                        }
                        $push_email = db::name(self::$member_tb['member'])->where(['uid'=>$data['push_uid']])->value('email');
                        single_send_mail($push_email,$v['email_subject'], $v['email_message'], $template = null , $attachment = null , $ssl = false , $from = null,$param=[]);
                    }
                }elseif($k === 'pc'){
                    $v['addtime'] = request()->time();
                    Db::name('msg')->insert($v);
                }
            }


            return api_return(11519);
        }catch (\Exception $e){
            throw $e;
            return api_return(-11519);
        }
    }






}